<?php

namespace App\Admin\Exports;

use App\Model\AppChannel;
use App\Model\CallBase;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class StatDailyExport extends ExcelExporter implements WithMapping, WithHeadings, ShouldAutoSize
{
    //这里是导出来的文件的名字和格式
    protected $fileName = '日报.xlsx';
    //这里是excel的标题
    /**
     * $grid->dt('日期');
        $grid->channel('注册渠道')->display(function ($data) use ($channels) {
            if ($data == 'all') return '全部渠道';
            return $channels[$data] ?? '-';
        });
        $grid->recharge_rmb('充值金额（元）')->display(function ($data) {
            return round($data / 100, 1);
        })->sortable();
        $grid->active_total('活跃用户总数');
        $grid->active_male('活跃男用户');
        $grid->active_host('活跃女用户');
        $grid->reg_total('注册用户');
        $grid->reg_male('注册男用户');
        $grid->reg_female('注册女用户');
        $grid->recharge_num('充值用户');
        $grid->recharge_coin('充值金币数');
        $grid->new_recharge_num('新增充值用户');
        $grid->new_recharge_coin('新增充值金额（金币）');
        $grid->consume_num('消费用户');
        $grid->consume_coin('消费金额（金币）');
        $grid->withdraw_num('提现人数');
        $grid->withdraw_coin('提现金额（金币）');
        $grid->total_user_num('总用户数（不含未完成注册的）');
        $grid->total_host_num('总女用户数');
     */
    protected $headings = [
        'dt' => '日期',
        'channel' => '渠道',
        'recharge_rmb' => '充值金额（元）',
        'active_total' => '活跃用户总数',
        'active_male' => '活跃男用户',
        'active_host' => '活跃女用户',
        'reg_total' => '注册用户',
        'reg_male' => '注册男用户',
        'reg_female' => '注册女用户',
        'reg_host' => '认证主播数',
        'recharge_num' => '充值用户',
        'recharge_coin' => '充值金币数',
        'new_recharge_num' => '新增充值用户',
        'new_recharge_rmb' => '新增充值金额（元）',
        'consume_num' => '消费用户',
        'consume_coin' => '消费金额（金币）',
        'withdraw_num' => '提现人数',
        'withdraw_coin' => '提现金币',
        'withdraw_rmb' => '提现金额（元）',
        'total_user_num' => '总用户数（不含未完成注册的）',
        'total_host_num' => '总女用户数',
    ];

    // public function headings(): array
    // {
    //     return ;
    // }

    public function map($data): array
    {
        $arr = [];
        $channels = AppChannel::pluck('channel_name', 'channel');
        foreach ($this->headings as $key => $item) {
            $tmp = $data->{$key};
            if (in_array($key, ['recharge_rmb', 'new_recharge_rmb'])) $tmp = round($tmp / 100, 1);
            if ($key == 'channel') {
                if ($tmp == 'all') $tmp = '全部渠道';
                else $tmp = $channels[$tmp] ?? '-';
            }
            if ($key == 'withdraw_rmb') {
                $tmp = round($data->withdraw_coin * 0.1 * config('busi.withdraw_percent'), 1);
            }
            $arr[] = (string)$tmp;
        }
        return $arr;
    }
}
